package com.everflourish.yeah100.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.everflourish.yeah100.R;
import com.everflourish.yeah100.act.statistics.TeacherStatisticsActivity;
import com.everflourish.yeah100.entity.statistics.ClassRanksModel;
import com.everflourish.yeah100.utils.MyToast;

import java.util.List;

/**
 * 作者：Created by liqisi on 2016/8/5.
 * 时间：2016/8/5
 */
public class ClassRanksAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private Context mContext;
    private List<ClassRanksModel> mList;
    private TeacherStatisticsActivity mActivity;

    public ClassRanksAdapter(Context mContext, TeacherStatisticsActivity mActivity, List<ClassRanksModel> mList) {
        this.mContext = mContext;
        this.mList = mList;
        this.mActivity = mActivity;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ClassRanksModel classRanksModel = mList.get(position);
        boolean isChecked = classRanksModel.isChecked();
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_statistics_filter_selected,
                    parent, false);
            viewHolder.mItemLl = (LinearLayout) convertView.findViewById(R.id.statistics_drawer_item_ll);
            viewHolder.mItemTv = (TextView) convertView.findViewById(R.id.statistics_drawer_list_tv);
            viewHolder.mItemLl.setOnClickListener(new MyOnClickListener(viewHolder, position));
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            if (isChecked) {
                convertView.setBackgroundColor(mContext.getResources().getColor(R.color.statistics_drawer_item_selector));
            } else {
                convertView.setBackgroundResource(R.drawable.selector_drawer_list);
            }
        }
        return convertView;
    }

    class MyOnClickListener implements View.OnClickListener {

        private ViewHolder holder;
        private int position;

        public MyOnClickListener(ViewHolder holder, int position) {
            this.holder = holder;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.statistics_drawer_item_ll:
                    boolean isChecked = mList.get(position).isChecked();
                    if (isChecked) {
                        mList.get(position).setIsChecked(!isChecked);
                        MyToast.showShort(mContext, "哈哈哈");
                    } else {
                        MyToast.showShort(mContext, "呵呵呵");
                        mList.get(position).setIsChecked(!isChecked);
                    }
                    notifyDataSetChanged();
                    mActivity.mClassRanksEt.setText(mList.get(position).getClassRanks().getMaxRank() + "");
                    break;
            }
        }
    }

    public class ViewHolder {
        private LinearLayout mItemLl;
        private TextView mItemTv;
    }
}
